/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.game.math.view.domain;

import java.util.Observable;
import java.util.Observer;
import org.game.math.view.config.Configure;
import org.game.math.view.expression.GenerateExpression;
import math._;

/**
 *
 * @author nmduc
 */
class Examination extends Observable with Observer {

  var expressionList:List[MathExpression] = List();
  var checked:Boolean = false;
  Configure.getInstance().addObserver(this);
  createExpression(Configure.getInstance());

  @Override
  def update(o:Observable , o1:Object ):Unit = {
    this.createExpression( o.asInstanceOf[Configure]);
  }

  def reset():Unit = {
    this.createExpression(Configure.getInstance());
  }

  private def createExpression(configure:Configure):Unit = {
    if (configure.isChosenMultiTable()) {
      this.createMultiplyTable(configure.getChosenMultiNumber());
    } else {
      val sign:String = configure.getProperty(Configure.SIGN);
      val numExpression:String = configure.getProperty(Configure.NUM_EXPRESSION);
      val numLength:String = configure.getProperty(Configure.NUM_LENGTH);
      val numOperator:String = configure.getProperty(Configure.NUM_OPERATOR);
      var operatorSet:Set[Enumeration#Value] = configure.getOperators();
      this.expressionList= GenerateExpression.getInstance().generateExpression(
        numExpression.toInt, operatorSet, numOperator.toInt,
        sign.toBoolean, numLength.toInt);
      this.setChanged();
      this.notifyObservers();
    }
    this.checked = false;
  }

  private def createMultiplyTable(multipliedNumber:Int):Unit = {
    this.expressionList = GenerateExpression.getInstance().generateMultiplyTable(multipliedNumber);
    this.setChanged();
    this.notifyObservers();
  }

  def calculatePointsAndStopEditing():Long = {
    var corrects:Int = 0;

    corrects += (expressionList filter {_.corrected}).size;
    
//    corrects += expressionList.filter({_.corrected}).size;


//    for (expresion <-  expressionList) {
//      if (expresion.corrected) {
//        corrects +=1;
//      }
//    }
    val points:Long = round(corrects * 10.0 / expressionList.size);
    this.checked=true;
    this.setChanged();
    this.notifyObservers();
    points;
  }
}

object Examination {
  val examination:Examination = new Examination();
  def getInstance():Examination = {
    Examination.examination;
  }
}